<template>
  <div class="todolist">
    <div class="top">
      <div class="group">
        <span>ToDoList</span>
        <input @keyup.enter="addItem" type="text" v-model="inputValue" value placeholder="添加ToDo" />
      </div>
    </div>
    <div class="content">
      <p>正在进行</p>
      <div class="global-delete" @click="delAll">删除选中部分</div>
      <div class="global-delete" @click="changeState">变更状态</div>
      <span>Checked names: {{ checkedNames }}</span>
      <span v-if="list.length <= 0" class="zero">暂无列表信息</span>
      <transition-group appear name="list-completes" class="types" tag="ul">
        <li @click="removeItem(index)" v-for="(item, index) of list" :key="index" class="list-complete-items">
          <div>
            <input type="checkbox" @click="on($event)" :value="index" v-model="checkedNames" />
            <span class="item">{{ item }}</span>
            <span class="btn" @click.stop="editItem(index)">编辑</span>
            <span
              class="btn"
              @click.stop="addCollections(index)"
              style="background:red;"
            >{{collections[index] === -1 ? "收藏一下" : "取消收藏"}}</span>
          </div>
        </li>
      </transition-group>
      <p>已经完成</p>
      <ul class="finish">
        <transition-group appear name="list-complete">
          <li class="list-complete-item" v-for="(item, index) of finishlist" :key="index">
            <span class="finishtext">{{ item }}</span>
          </li>
        </transition-group>
      </ul>
    </div>
    <home-bar></home-bar>
  </div>
</template>

<script>
import { mapActions, mapState, mapGetters } from "vuex";
import HomeBar from "@/components/tab-bar/tab-bar";
export default {
  name: "ToDoList",
  components: {
    HomeBar
  },
  props: {},
  data() {
    return {
      inputValue: "",
      checkedNames: []
    };
  },
  computed: {
    ...mapState("todo", ["list", "finishlist", "collections"])
  },
  methods: {
    addItem(e) {
      const value = e.target.value.trim();
      // this.$store.state.todo.list.push(value);
      this.$store.dispatch("todo/addItem", { value });
      this.inputValue = "";
    },
    removeItem(index) {
      console.log(index);
      this.$store.dispatch("todo/removeItem", { editIndex: index });
    },
    editItem(index) {
      const val = this.$store.state.todo.list[index];
      const editVal = prompt("您确定要修改第" + index + "个的数据吗?", val);
      if (editVal !== null) {
        this.$store.dispatch("todo/editItem", { index, editVal });
      }
    },
    on(e) {
      e.cancelBubble = true;
    },
    delAll() {
      const index = this.checkedNames;
      this.$store.dispatch("todo/delAll", { delAll: index });
      this.checkedNames = [];
    },
    changeState() {
      const index = this.checkedNames;
      this.$store.dispatch("todo/changeState", { changeArray: index });
      this.checkedNames = [];
    },
    addCollections(index) {
      this.$store.dispatch("todo/addCollections", { index });
    }
  },
  watch: {
    list: {
      handler: function(val, oldVal) {
        localStorage.setItem("list", JSON.stringify(val));
        localStorage.setItem("collections", JSON.stringify(this.collections));
      },
      deep: true
    },
    finishlist: {
      handler: function(val, oldVal) {
        localStorage.setItem("finishlist", JSON.stringify(val));
      },
      deep: true
    }
  }
};
</script>
<style lang="scss" scoped>
.list-complete-item {
  transition: all 1s;
  display: inline-block;
}
.list-complete-enter, .list-complete-leave-to {
  opacity: 0;
  transform: translateY(30px);
}
.list-complete-leave-active {
  position: absolute;
}
.list-complete-items {
  transition: all 1s;
}
.list-completes-enter, .list-completes-leave-to {
  opacity: 0;
  transform: translateY(-30px);
}
.list-completes-leave-active {
  position: absolute;
}
.todolist {
  margin: 0;
  padding: 0;
  .top {
    width: 100%;
    height: 50px;
    background-color: #000000;
    .group {
      max-width: 600px;
      height: 50px;
      margin: 0 auto;
      line-height: 50px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      span {
        font-size: 20px;
        color: #ffffff;
      }
      input[type="text"] {
        padding-left: 15px;
        font-size: 15px;
        width: 300px;
        height: 30px;
        margin-top: 10px;
        border-radius: 5px;
        box-sizing: border-box;
        outline: none;
        box-shadow: 0 1px 0 rgba(255, 255, 255, 0.24),
          0 1px 6px rgba(0, 0, 0, 0.45) inset;
        border: none;
      }
    }
  }
  .content {
    max-width: 600px;
    margin: 0 auto;
    .global-delete {
      font-size: 15px;
      width: 130px;
      height: 30px;
      line-height: 30px;
      background-color: #000000;
      color: #ffffff;
      text-align: center;
      border-radius: 5px;
      font-family: "Microsoft YaHei";
      cursor: pointer;
      margin-top: 10px;
    }
    .zero {
      font-size: 30px;
      font-family: "Microsoft YaHei";
    }
    p {
      font-size: 25px;
      font-weight: bold;
    }
    .types li {
      div {
        width: 100%;
        font-size: 20px;
        height: 32px;
        line-height: 32px;
        background: #fff;
        position: relative;
        margin-bottom: 10px;
        padding: 0 45px;
        border-radius: 3px;
        border-left: 5px solid #629a9c;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07);
        display: inline-flex;
        flex-direction: row;
        justify-content: space-between;
        .item {
          align-self: flex-start;
        }
        .btn {
          color: #fff;
          background-color: #337ab7;
          border-color: #2e6da4;
          border-radius: 4px;
          font-size: 16px;
          padding: 0 5px;
        }
      }
    }
    .finish li {
      width: 100%;
      font-size: 20px;
      height: 32px;
      line-height: 32px;
      background: #fff;
      position: relative;
      margin-bottom: 10px;
      padding: 0 45px;
      border-radius: 3px;
      border-left: 5px solid #629a9c;
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07);
      display: inline-flex;
      flex-direction: row;
      justify-content: space-between;
      .finishtext {
        text-decoration: line-through;
        color: #969799;
      }
      .btn {
        color: #fff;
        background-color: #337ab7;
        border-color: #2e6da4;
        border-radius: 4px;
        font-size: 16px;
        padding: 0 5px;
      }
    }
  }
}
</style>
